﻿namespace FR.SAFTAnalyser.ViewModels.Workspace
{
    using FR.SAFTAnalyser.Services;
    using FR.SAFTAnalyser.ViewModels.Pane;
    using System;
    using System.Threading;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Threading;

    internal partial class Workspace
    {
        private bool Closing_CanExecute()
        {
            if (this.mShutDownInProgress == true)
                return false;

            // Check if conditions within the WorkspaceViewModel are suitable to close the application
            // eg.: Prompt to Cancel long running background tasks such as Search - Replace in Files (if any)

            return true;
        }

        /// <summary>
        /// Bind a window to some commands to be executed by the viewmodel.
        /// </summary>
        /// <param name="win"></param>
        public void InitCommandBinding(Window win)
        {
            #region Exit
            win.CommandBindings.Add(new CommandBinding(AppCommand.Exit,
                (s, e) =>
                {
                    this.AppExit_CommandExecuted();
                }));
            #endregion Exit

            #region LoadSAFT
            win.CommandBindings.Add(new CommandBinding(AppCommand.LoadSAFT,
                (s, e) =>
                {
                    try
                    {
                        ServiceResolver.Instance.OpenFileService.Filter = "SAF-T|*.xml";
                        ServiceResolver.Instance.OpenFileService.FileName = "";

                        if (ServiceResolver.Instance.OpenFileService.ShowDialog(null).GetValueOrDefault())
                            this.OpenSaftFile(ServiceResolver.Instance.OpenFileService.FileName);
                    }
                    catch (Exception exp)
                    {
                        ServiceResolver.Instance.MessageBoxService.ShowError(exp.Message, App.IssueTrackerText);
                    }
                }));
            #endregion LoadSAFT

            #region LoadPrivatekey
            win.CommandBindings.Add(new CommandBinding(AppCommand.LoadPrivateKeyFile,
                (s, e) =>
                {
                    try
                    {
                        ServiceResolver.Instance.OpenFileService.Filter = "Chave privada|*.pem;*.txt";
                        ServiceResolver.Instance.OpenFileService.FileName = "";

                        if (ServiceResolver.Instance.OpenFileService.ShowDialog(null).GetValueOrDefault())
                            this.OpenPrivateKeyFile(ServiceResolver.Instance.OpenFileService.FileName);
                    }
                    catch (Exception exp)
                    {
                        ServiceResolver.Instance.MessageBoxService.ShowError(exp.Message, App.IssueTrackerText);
                    }
                }));
            #endregion LoadFile

            #region LoadPublickey
            win.CommandBindings.Add(new CommandBinding(AppCommand.LoadPublicKeyFile,
                (s, e) =>
                {
                    try
                    {
                        ServiceResolver.Instance.OpenFileService.Filter = "Chave pública|*.pem;*.txt";
                        ServiceResolver.Instance.OpenFileService.FileName = "";

                        if (ServiceResolver.Instance.OpenFileService.ShowDialog(null).GetValueOrDefault())
                            this.OpenPublicKeyFile(ServiceResolver.Instance.OpenFileService.FileName);
                    }
                    catch (Exception exp)
                    {
                        ServiceResolver.Instance.MessageBoxService.ShowError(exp.Message, App.IssueTrackerText);
                    }
                }));
            #endregion LoadFile

            #region LoadStocks
            win.CommandBindings.Add(new CommandBinding(AppCommand.LoadStockFile,
                (s, e) =>
                {
                    try
                    {
                        ServiceResolver.Instance.OpenFileService.Filter = "Ficheiro Xml (*.xml)|*.xml|Csv (*.csv)|*.csv";
                        ServiceResolver.Instance.OpenFileService.FileName = "";

                        if (ServiceResolver.Instance.OpenFileService.ShowDialog(null).GetValueOrDefault())
                            this.OpenStockFile(ServiceResolver.Instance.OpenFileService.FileName);
                    }
                    catch (Exception exp)
                    {
                        ServiceResolver.Instance.MessageBoxService.ShowError(exp.Message, App.IssueTrackerText);
                    }
                }));
            #endregion LoadStocks
        }

        private bool CanExecuteIfActiveDocumentIsFileViewModel()
        {
            return this.ActiveDocument != null && this.ActiveDocument is DocumentPartViewModel;
        }
    }
}
